module rank {
    export class PushButton extends egret.DisplayObjectContainer {

        private bitmap: egret.Bitmap;

        public static CLICK: string = "push_button_click";

        public constructor(bitmap: egret.Bitmap) {
            super();
            this.bitmap = bitmap;
            this.bitmap.touchEnabled = true;
            this.bitmap.anchorOffsetX = this.bitmap.width / 2;
            this.bitmap.anchorOffsetY = this.bitmap.height / 2;
            this.bitmap.addEventListener(egret.TouchEvent.TOUCH_BEGIN, this.onBegin, this);
            this.addChild(this.bitmap);
            this.bitmap.x += this.bitmap.width / 2;
            this.bitmap.y += this.bitmap.height / 2;
        }

        private onBegin(eve: egret.TouchEvent): void {
            eve.stopImmediatePropagation();
            eve.stopPropagation();
            this.bitmap.addEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this);
            this.bitmap.addEventListener(egret.TouchEvent.TOUCH_CANCEL, this.onTouchCancel, this);
            this.bitmap.addEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, this.onTouchCancel, this);
            this.bitmap.scaleX = this.bitmap.scaleY = 0.8;
        }

        private onTouchEnd(eve: egret.TouchEvent): void {
            eve.stopImmediatePropagation();
            eve.stopPropagation();
            this.bitmap.removeEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this);
            this.bitmap.removeEventListener(egret.TouchEvent.TOUCH_CANCEL, this.onTouchCancel, this);
            this.bitmap.removeEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, this.onTouchCancel, this);
            this.bitmap.scaleX = this.bitmap.scaleY = 1;
            this.dispatchEvent(new egret.Event(PushButton.CLICK));
        }

        private onTouchCancel(eve: egret.TouchEvent): void {
            eve.stopImmediatePropagation();
            eve.stopPropagation();
            this.bitmap.removeEventListener(egret.TouchEvent.TOUCH_END, this.onTouchEnd, this);
            this.bitmap.removeEventListener(egret.TouchEvent.TOUCH_CANCEL, this.onTouchCancel, this);
            this.bitmap.removeEventListener(egret.TouchEvent.TOUCH_RELEASE_OUTSIDE, this.onTouchCancel, this);
            this.bitmap.scaleX = this.bitmap.scaleY = 1;
        }

    }
}